﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.Collections.ObjectModel;
using ARR.UI.InventoriesService;
using ARR.UI.CategoriesService;
using System.Windows.Input;
using ARR.UI.Commands;
using ARR.UI.ArticlesService;

namespace ARR.UI.ViewModels
{
    public class MissingArticlesPopupViewModel : ViewModelBase
    {
        private ICommand saveCommand;
        private ICommand cancelCommand;


        public MissingArticlesPopupViewModel()
        {
            LoadCategories();
            LoadMissingArticles();
        }


        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(param => Save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(param => Cancel());
                }
                return cancelCommand;
            }
        }


        public ObservableCollection<Article> Articles { get; set; }

        public List<Category> Categories { get; set; }



        private void Save()
        {
            if (CanSave())
            {
                ArticlesServiceClient articlesService = new ArticlesServiceClient();
                InventoriesServiceClient inventoriesService = new InventoriesServiceClient();

                try
                {
                    articlesService.SaveArticles(Articles.ToList());
                    articlesService.Close();

                    try
                    {
                        inventoriesService.TempInvetoriesUpdateIds(WorkspaceData.Instance.RangeReview.Id);
                        inventoriesService.Close();
                    }
                    catch
                    {
                        inventoriesService.Abort();
                        throw;
                    }
                }
                catch
                {
                    articlesService.Abort();
                    throw;
                }

                Helpers.WindowManager.CloseView(this);
            }
            else
            {
                throw new ARRException("Debe marcar todos los articulos como nuevo o delistado. Tambien debe completar los datos faltantes de cada articulo.");
            }
        }

        private bool CanSave()
        {
            foreach (Article art in Articles)
            {
                if (!art.IsValid)
                    return false;

                if (!art.IsDelisted && !art.IsNew)
                    return false;
            }

            return true;
        }

        private void Cancel()
        {
            LoadMissingArticles();
        }

        private void LoadMissingArticles()
        {
            InventoriesServiceClient service = new InventoriesServiceClient();

            try
            {
                Articles = new ObservableCollection<Article>(service.GetNewArticles(WorkspaceData.Instance.RangeReview.Id));
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

            foreach (var art in Articles)
            {
                art.RangeReviewId = WorkspaceData.Instance.RangeReview.Id;
            }
        }

        private void LoadCategories()
        {
            List<Category> categories = new List<Category>();

            CategoriesServiceClient categoryService = new CategoriesServiceClient();
            try
            {
                categories = categoryService.GetByLevels(2);
                categoryService.Close();
            }
            catch
            {
                categoryService.Abort();
                throw;
            }

            foreach (var category in categories.Where(x => x.Level == 2))
            {
                category.SubCategories = new List<Category>();
                category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
            }

            Categories = categories.Where(x => x.Level == 2).OrderBy(x => x.Name).ToList();
        }
    }
}
